package cz.muni.fi.pa165.musiclibrary.musiclibraryclient;

import cz.muni.fi.pa165.musiclibrary.Artist;
import cz.muni.fi.pa165.musiclibrary.Genre;
import cz.muni.fi.pa165.musiclibrary.MusicLibraryException_Exception;
import cz.muni.fi.pa165.musiclibrary.MusicLibraryWS;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import javax.swing.text.DefaultCaret;
import javax.xml.ws.WebServiceException;
import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.endpoint.Client;


/**
 * GUI for Music library client
 * 
 * @author Radoslav Stefanik
 */
public class MusicLibraryClientApplication extends javax.swing.JFrame {

    
    MusicLibraryWS musicLibrary;
    Client client;
    Artist selectedArtist;
    Genre selectedGenre;
    
    /**
     * Creates new form MusicLibraryClientApplication
     */
    public MusicLibraryClientApplication() {
        initComponents();
        jConnectButton.setVisible(false);
        try {
            musicLibrary = new cz.muni.fi.pa165.musiclibrary.musiclibraryws.MusicLibraryWS().getMusicLibraryWSImplPort();
            musicLibrary.login("rest", "rest");
        } catch(WebServiceException ex) {
            jStatusTextArea.append("Error while communicating with server: " + ex.getMessage()+ "\n");
            disableFunctionality();
            jConnectButton.setVisible(true);
        } catch (MusicLibraryException_Exception ex) {
            jStatusTextArea.append("Incorrect user name or password.");
        }
        this.setLocationRelativeTo(null);
        jArtistDialog.setSize(300, 400);
        jGenreDialog.setSize(300,200);
        ((DefaultCaret)jStatusTextArea.getCaret()).setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
        try {
            this.setIconImage(ImageIO.read(getClass().getResource("/icon.png")));
            jArtistDialog.setIconImage(ImageIO.read(getClass().getResource("/icon.png")));
            jGenreDialog.setIconImage(ImageIO.read(getClass().getResource("/icon.png")));
        } catch (IOException ex) {
            
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        typeSelectionGroup = new javax.swing.ButtonGroup();
        jArtistDialog = new javax.swing.JDialog();
        jArtistCloseButton = new javax.swing.JButton();
        jArtistSaveButton = new javax.swing.JButton();
        jArtistNameLabel = new javax.swing.JLabel();
        jArtistNameTextField = new javax.swing.JTextField();
        jArtistDescriptionLabel = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jArtistDescriptionTextArea = new javax.swing.JTextArea();
        jArtistErrorLabel = new javax.swing.JLabel();
        jGenreDialog = new javax.swing.JDialog();
        jLabel1 = new javax.swing.JLabel();
        jGenreNameTextField = new javax.swing.JTextField();
        jGenreCloseButton = new javax.swing.JButton();
        jGenreSaveButton = new javax.swing.JButton();
        jGenreErrorLabel = new javax.swing.JLabel();
        jUpPanel = new javax.swing.JPanel();
        jLogoImageLabel = new javax.swing.JLabel();
        jConnectButton = new javax.swing.JButton();
        jDownPanel = new javax.swing.JPanel();
        jStatusScrollPane = new javax.swing.JScrollPane();
        jStatusTextArea = new javax.swing.JTextArea();
        jMainPanel = new javax.swing.JPanel();
        jRadioSelectionPanel = new javax.swing.JPanel();
        jRadioButtonArtist = new javax.swing.JRadioButton();
        jRadioButtonGenre = new javax.swing.JRadioButton();
        jItemsScrollPane = new javax.swing.JScrollPane();
        jItemList = new javax.swing.JList();
        jViewButton = new javax.swing.JButton();
        jCreateButton = new javax.swing.JButton();
        jDeleteButton = new javax.swing.JButton();

        jArtistDialog.setTitle("Artist");
        jArtistDialog.setModal(true);

        jArtistCloseButton.setText("Close");
        jArtistCloseButton.setToolTipText("");
        jArtistCloseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jArtistCloseButtonActionPerformed(evt);
            }
        });

        jArtistSaveButton.setText("Save");
        jArtistSaveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jArtistSaveButtonActionPerformed(evt);
            }
        });

        jArtistNameLabel.setText("Name");

        jArtistDescriptionLabel.setText("Description");

        jArtistDescriptionTextArea.setColumns(20);
        jArtistDescriptionTextArea.setLineWrap(true);
        jArtistDescriptionTextArea.setRows(5);
        jScrollPane4.setViewportView(jArtistDescriptionTextArea);

        jArtistErrorLabel.setForeground(new java.awt.Color(255, 0, 0));

        javax.swing.GroupLayout jArtistDialogLayout = new javax.swing.GroupLayout(jArtistDialog.getContentPane());
        jArtistDialog.getContentPane().setLayout(jArtistDialogLayout);
        jArtistDialogLayout.setHorizontalGroup(
            jArtistDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jArtistDialogLayout.createSequentialGroup()
                .addGroup(jArtistDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jArtistDialogLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jArtistDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jArtistDialogLayout.createSequentialGroup()
                                .addGap(0, 200, Short.MAX_VALUE)
                                .addComponent(jArtistSaveButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jArtistCloseButton))
                            .addGroup(jArtistDialogLayout.createSequentialGroup()
                                .addGroup(jArtistDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jArtistNameLabel)
                                    .addComponent(jArtistDescriptionLabel))
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(jArtistDialogLayout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addGroup(jArtistDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jArtistDialogLayout.createSequentialGroup()
                                        .addComponent(jArtistErrorLabel)
                                        .addGap(0, 0, Short.MAX_VALUE))
                                    .addComponent(jArtistNameTextField)))))
                    .addGroup(jArtistDialogLayout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jScrollPane4)))
                .addContainerGap())
        );
        jArtistDialogLayout.setVerticalGroup(
            jArtistDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jArtistDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jArtistNameLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jArtistErrorLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jArtistNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jArtistDescriptionLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jArtistDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jArtistCloseButton)
                    .addComponent(jArtistSaveButton))
                .addContainerGap())
        );

        jGenreDialog.setTitle("Genre");
        jGenreDialog.setAlwaysOnTop(true);
        jGenreDialog.setModal(true);

        jLabel1.setText("Name");

        jGenreCloseButton.setText("Close");
        jGenreCloseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jGenreCloseButtonActionPerformed(evt);
            }
        });

        jGenreSaveButton.setText("Save");
        jGenreSaveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jGenreSaveButtonActionPerformed(evt);
            }
        });

        jGenreErrorLabel.setForeground(new java.awt.Color(255, 0, 0));

        javax.swing.GroupLayout jGenreDialogLayout = new javax.swing.GroupLayout(jGenreDialog.getContentPane());
        jGenreDialog.getContentPane().setLayout(jGenreDialogLayout);
        jGenreDialogLayout.setHorizontalGroup(
            jGenreDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jGenreDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jGenreDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jGenreDialogLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jGenreDialogLayout.createSequentialGroup()
                        .addGap(0, 121, Short.MAX_VALUE)
                        .addComponent(jGenreSaveButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jGenreCloseButton))
                    .addGroup(jGenreDialogLayout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(jGenreDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jGenreDialogLayout.createSequentialGroup()
                                .addComponent(jGenreErrorLabel)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(jGenreNameTextField))))
                .addContainerGap())
        );
        jGenreDialogLayout.setVerticalGroup(
            jGenreDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jGenreDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jGenreErrorLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jGenreNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 117, Short.MAX_VALUE)
                .addGroup(jGenreDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jGenreCloseButton)
                    .addComponent(jGenreSaveButton))
                .addContainerGap())
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Music Library Client");
        setMinimumSize(new java.awt.Dimension(300, 400));

        jLogoImageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pa-165-design-client-small.png"))); // NOI18N

        jConnectButton.setText("Connect");
        jConnectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jConnectButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jUpPanelLayout = new javax.swing.GroupLayout(jUpPanel);
        jUpPanel.setLayout(jUpPanelLayout);
        jUpPanelLayout.setHorizontalGroup(
            jUpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jUpPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLogoImageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jConnectButton)
                .addContainerGap())
        );
        jUpPanelLayout.setVerticalGroup(
            jUpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLogoImageLabel)
            .addComponent(jConnectButton)
        );

        jDownPanel.setMinimumSize(new java.awt.Dimension(40, 40));
        jDownPanel.setName(""); // NOI18N

        jStatusScrollPane.setMinimumSize(new java.awt.Dimension(30, 30));

        jStatusTextArea.setEditable(false);
        jStatusTextArea.setColumns(20);
        jStatusTextArea.setFont(new java.awt.Font("Monospaced", 0, 11)); // NOI18N
        jStatusTextArea.setForeground(new java.awt.Color(51, 51, 51));
        jStatusTextArea.setLineWrap(true);
        jStatusTextArea.setRows(5);
        jStatusScrollPane.setViewportView(jStatusTextArea);

        javax.swing.GroupLayout jDownPanelLayout = new javax.swing.GroupLayout(jDownPanel);
        jDownPanel.setLayout(jDownPanelLayout);
        jDownPanelLayout.setHorizontalGroup(
            jDownPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDownPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jStatusScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jDownPanelLayout.setVerticalGroup(
            jDownPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jStatusScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 52, Short.MAX_VALUE)
        );

        jMainPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        typeSelectionGroup.add(jRadioButtonArtist);
        jRadioButtonArtist.setText("Artist");
        jRadioButtonArtist.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonArtistActionPerformed(evt);
            }
        });

        typeSelectionGroup.add(jRadioButtonGenre);
        jRadioButtonGenre.setText("Genre");
        jRadioButtonGenre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonGenreActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jRadioSelectionPanelLayout = new javax.swing.GroupLayout(jRadioSelectionPanel);
        jRadioSelectionPanel.setLayout(jRadioSelectionPanelLayout);
        jRadioSelectionPanelLayout.setHorizontalGroup(
            jRadioSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jRadioSelectionPanelLayout.createSequentialGroup()
                .addComponent(jRadioButtonArtist)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButtonGenre)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jRadioSelectionPanelLayout.setVerticalGroup(
            jRadioSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jRadioSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jRadioButtonArtist)
                .addComponent(jRadioButtonGenre))
        );

        jItemList.setFont(new java.awt.Font("High Tower Text", 0, 13)); // NOI18N
        jItemList.setModel(new ArtistListModel());
        jItemList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jItemList.setCellRenderer(new ArtistListRenderer());
        jItemsScrollPane.setViewportView(jItemList);

        jViewButton.setText("View");
        jViewButton.setToolTipText("");
        jViewButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jViewButtonActionPerformed(evt);
            }
        });

        jCreateButton.setText("New");
        jCreateButton.setToolTipText("");
        jCreateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCreateButtonActionPerformed(evt);
            }
        });

        jDeleteButton.setText("Delete");
        jDeleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jDeleteButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jMainPanelLayout = new javax.swing.GroupLayout(jMainPanel);
        jMainPanel.setLayout(jMainPanelLayout);
        jMainPanelLayout.setHorizontalGroup(
            jMainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jMainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jMainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jItemsScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                    .addComponent(jRadioSelectionPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jMainPanelLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jDeleteButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCreateButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jViewButton)))
                .addContainerGap())
        );
        jMainPanelLayout.setVerticalGroup(
            jMainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jMainPanelLayout.createSequentialGroup()
                .addComponent(jRadioSelectionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jItemsScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jMainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jViewButton)
                    .addComponent(jCreateButton)
                    .addComponent(jDeleteButton)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jDownPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jUpPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jMainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jUpPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jMainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jDownPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jRadioButtonGenreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonGenreActionPerformed
        jItemList.setModel(new GenreListModel());
        jItemList.setCellRenderer(new GenreListRenderer());
        refreshGenreItemList();
    }//GEN-LAST:event_jRadioButtonGenreActionPerformed

    private void jCreateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCreateButtonActionPerformed
        if(jRadioButtonArtist.isSelected()){
            selectedArtist = new Artist();
            updateArtistDialogValue();
            jArtistDialog.setLocationRelativeTo(this);
            jArtistDialog.setVisible(true);         
        } else if(jRadioButtonGenre.isSelected()) {
            selectedGenre = new Genre();
            jGenreNameTextField.setText(selectedGenre.getName());
            jGenreDialog.setLocationRelativeTo(this);
            jGenreDialog.setVisible(true);
        } else {
            jStatusTextArea.append("Please select between Artist or Genre\n");
        }
    }//GEN-LAST:event_jCreateButtonActionPerformed

    private void jArtistSaveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jArtistSaveButtonActionPerformed
        selectedArtist.setName(jArtistNameTextField.getText());
        if(StringUtils.isBlank(selectedArtist.getName())) {
            jArtistErrorLabel.setText("Name should not be blank");
            return;
        }
        selectedArtist.setDescription(jArtistDescriptionTextArea.getText());
        if(selectedArtist.getIdArtist() == null) {
            try {
                musicLibrary.createArtist(selectedArtist);
                jStatusTextArea.append("artist successfully created\n");
            } catch (MusicLibraryException_Exception ex) {
                jStatusTextArea.append("Error while creating artist: " + ex.getMessage() + "\n");
            } catch (WebServiceException ex) {
                jStatusTextArea.append("Error while communicating with server: " + ex.getMessage()+ "\n");
            }
        } else {
            try {
                musicLibrary.updateArtist(selectedArtist);
                jStatusTextArea.append("artist successfully updated\n");
            } catch (MusicLibraryException_Exception ex) {
                jStatusTextArea.append("Error while updating artist: " + ex.getMessage() + "\n");
            } catch (WebServiceException ex) {
                jStatusTextArea.append("Error while communicating with server: " + ex.getMessage()+ "\n");
            }
        }
        jArtistErrorLabel.setText("");
        jArtistDialog.setVisible(false);
        refreshArtistItemList();
    }//GEN-LAST:event_jArtistSaveButtonActionPerformed

    private void jViewButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jViewButtonActionPerformed
        int index = jItemList.getSelectedIndex();
        if (index == -1) {
            jStatusTextArea.append("No item selected\n");
            return;
        }
        if(jRadioButtonArtist.isSelected()) {
            selectedArtist = (Artist) jItemList.getModel().getElementAt(index);
            updateArtistDialogValue();
            jArtistDialog.setLocationRelativeTo(this);
            jArtistDialog.setVisible(true);           
        } else if(jRadioButtonGenre.isSelected()) {
            selectedGenre = (Genre) jItemList.getModel().getElementAt(index);
            jGenreNameTextField.setText(selectedGenre.getName());
            jGenreDialog.setLocationRelativeTo(this);
            jGenreDialog.setVisible(true);
        }
    }//GEN-LAST:event_jViewButtonActionPerformed

    private void jArtistCloseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jArtistCloseButtonActionPerformed
        jArtistErrorLabel.setText("");
        jArtistDialog.setVisible(false);
    }//GEN-LAST:event_jArtistCloseButtonActionPerformed

    private void jGenreCloseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jGenreCloseButtonActionPerformed
        jGenreErrorLabel.setText("");
        jGenreDialog.setVisible(false);
    }//GEN-LAST:event_jGenreCloseButtonActionPerformed

    private void jGenreSaveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jGenreSaveButtonActionPerformed
        selectedGenre.setName(jGenreNameTextField.getText());
        if(StringUtils.isBlank(selectedGenre.getName())) {
            jGenreErrorLabel.setText("Name should not be blank.");
            return;
        }
        if(selectedGenre.getIdGenre()==null) {
            try {
                musicLibrary.createGenre(selectedGenre);
                jStatusTextArea.append("genre successfully created\n");
            } catch (MusicLibraryException_Exception ex) {
                jStatusTextArea.append("Error while creating genre: " + ex.getMessage() + "\n");
            } catch (WebServiceException ex) {
                jStatusTextArea.append("Error while communicating with server: " + ex.getMessage()+ "\n");
            }
        } else {
            try {
                musicLibrary.updateGenre(selectedGenre);
                jStatusTextArea.append("genre successfully updated\n");
            } catch (MusicLibraryException_Exception ex) {
                jStatusTextArea.append("Error while updating genre: " + ex.getMessage() + "\n");
            } catch (WebServiceException ex) {
                jStatusTextArea.append("Error while communicating with server: " + ex.getMessage()+ "\n");
            }
        }
        jGenreErrorLabel.setText("");
        jGenreDialog.setVisible(false);
        refreshGenreItemList();
    }//GEN-LAST:event_jGenreSaveButtonActionPerformed

    private void jRadioButtonArtistActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonArtistActionPerformed
        jItemList.setModel(new ArtistListModel());
        jItemList.setCellRenderer(new ArtistListRenderer());
        refreshArtistItemList();
    }//GEN-LAST:event_jRadioButtonArtistActionPerformed

    private void jDeleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jDeleteButtonActionPerformed
        int index = jItemList.getSelectedIndex();
        if (index == -1) {
            jStatusTextArea.append("No item selected\n");
            return;
        }
        if(jRadioButtonArtist.isSelected()) {
            selectedArtist = (Artist) jItemList.getModel().getElementAt(index);
            try {
                musicLibrary.deleteArtist(selectedArtist);
            } catch (MusicLibraryException_Exception ex) {
                jStatusTextArea.append("Error: " + ex.getMessage()+ "\n");
            } catch (WebServiceException ex) {
                jStatusTextArea.append("Error while communicating with server: " + ex.getMessage()+ "\n");
            }
            refreshArtistItemList();
        } else if(jRadioButtonGenre.isSelected()) {
            selectedGenre = (Genre) jItemList.getModel().getElementAt(index);
            try {
                musicLibrary.deleteGenre(selectedGenre);
            } catch (MusicLibraryException_Exception ex) {
                jStatusTextArea.append("Error: " + ex.getMessage()+ "\n");
            } catch (WebServiceException ex) {
                jStatusTextArea.append("Error while communicating with server: " + ex.getMessage() + "\n");
            }
            refreshGenreItemList();
        }
    }//GEN-LAST:event_jDeleteButtonActionPerformed

    private void jConnectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jConnectButtonActionPerformed
        try {
            musicLibrary = new cz.muni.fi.pa165.musiclibrary.musiclibraryws.MusicLibraryWS().getMusicLibraryWSImplPort();
            jConnectButton.setVisible(false);
            enableFunctionality();
        } catch(WebServiceException ex) {
            jStatusTextArea.append("Error while communicating with server: " + ex.getMessage()+ "\n");
            disableFunctionality();
            jConnectButton.setVisible(true);
        }
    }//GEN-LAST:event_jConnectButtonActionPerformed

    /**
     * Sets values to Artist dialog according to selected artist
     */
    private void updateArtistDialogValue() {
        jArtistNameTextField.setText(selectedArtist.getName());
        jArtistDescriptionTextArea.setText(selectedArtist.getDescription());
    }
    
    /**
     * Forces to refresh Item list with artists
     */
    private void refreshArtistItemList() {
        if (jRadioButtonArtist.isSelected()) {
            Thread thread = new Thread() {
                @Override
                public void run() {
                    disableFunctionality();
                    try {
                        ((ArtistListModel) jItemList.getModel()).setArtists(musicLibrary.getArtists());
                        jItemList.setSelectedIndex(0);
                        jStatusTextArea.append("Artists loaded successfully\n");
                    } catch (MusicLibraryException_Exception ex) {
                        jStatusTextArea.append("Error: " + ex.getMessage()+ "\n");
                    } catch (WebServiceException ex) {
                        jStatusTextArea.append("Error while communicating with server: " + ex.getMessage() + "\n");
                    }
                    enableFunctionality();
                }
            };
            thread.start();
        }
    }

    /**
     * Forces to refresh Item list with genres
     */
    private void refreshGenreItemList() {
        if (jRadioButtonGenre.isSelected()) {
            Thread thread = new Thread() {
                @Override
                public void run() {
                    disableFunctionality();
                    try {
                        ((GenreListModel) jItemList.getModel()).setGenres(musicLibrary.getGenres());
                        jItemList.setSelectedIndex(0);
                        jStatusTextArea.append("Genres loaded successfully\n");
                    } catch (MusicLibraryException_Exception ex) {
                        jStatusTextArea.append("Error: " + ex.getMessage()+ "\n");
                    } catch (WebServiceException ex) {
                        jStatusTextArea.append("Error while communicating with server: " + ex.getMessage() + "\n");
                    }
                    enableFunctionality();
                }
            };
            thread.start();
        }
    }
    
    /**
     * disables group of buttons and elements
     */
    private void disableFunctionality() {
        jRadioButtonGenre.setEnabled(false);
        jRadioButtonArtist.setEnabled(false);
        jViewButton.setEnabled(false);
        jCreateButton.setEnabled(false);
        jDeleteButton.setEnabled(false);
    }
    
    /**
     * enables group of buttons and elements
     */
    private void enableFunctionality() {
        jRadioButtonGenre.setEnabled(true);
        jRadioButtonArtist.setEnabled(true);
        jViewButton.setEnabled(true);
        jCreateButton.setEnabled(true);
        jDeleteButton.setEnabled(true);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jArtistCloseButton;
    private javax.swing.JLabel jArtistDescriptionLabel;
    private javax.swing.JTextArea jArtistDescriptionTextArea;
    private javax.swing.JDialog jArtistDialog;
    private javax.swing.JLabel jArtistErrorLabel;
    private javax.swing.JLabel jArtistNameLabel;
    private javax.swing.JTextField jArtistNameTextField;
    private javax.swing.JButton jArtistSaveButton;
    private javax.swing.JButton jConnectButton;
    private javax.swing.JButton jCreateButton;
    private javax.swing.JButton jDeleteButton;
    private javax.swing.JPanel jDownPanel;
    private javax.swing.JButton jGenreCloseButton;
    private javax.swing.JDialog jGenreDialog;
    private javax.swing.JLabel jGenreErrorLabel;
    private javax.swing.JTextField jGenreNameTextField;
    private javax.swing.JButton jGenreSaveButton;
    private javax.swing.JList jItemList;
    private javax.swing.JScrollPane jItemsScrollPane;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLogoImageLabel;
    private javax.swing.JPanel jMainPanel;
    private javax.swing.JRadioButton jRadioButtonArtist;
    private javax.swing.JRadioButton jRadioButtonGenre;
    private javax.swing.JPanel jRadioSelectionPanel;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jStatusScrollPane;
    private javax.swing.JTextArea jStatusTextArea;
    private javax.swing.JPanel jUpPanel;
    private javax.swing.JButton jViewButton;
    private javax.swing.ButtonGroup typeSelectionGroup;
    // End of variables declaration//GEN-END:variables
}
